/*
 * @(#) Anagrams.java
 *
 */

import java.util.Scanner;
import java.util.Arrays;

/**
 * Are anagrams or not.
 *
 * @author Yaroslav Tabachnyy
 * @version 1.04
 * @date 27.01.2013
 * @mail Yaroslav.Tabachnyy@gmail.com
 *
 */
public class Anagrams {
    
    public static void main(String[] args) {
        
        String clear = normalizeString(inputData("Enter clear string:"));
        String scrambled = normalizeString(inputData("Enter scrambled string:"));
        
        outputResult(areAnagram(clear, scrambled));
        
    }
    
    private static String inputData(String messageToUser) {
        
        Scanner dataFromConsole = new Scanner(System.in);
        
        System.out.println(messageToUser);
        
        return (dataFromConsole.nextLine());
        
    }
    
    private static String normalizeString(String primaryString) {
        
        StringBuilder destinationString = new StringBuilder(primaryString.length());
        
        for (int i = (primaryString.length() - 1); i >= 0; i--) {
            
            if (Character.isLetterOrDigit(primaryString.codePointAt(i))) {
                
                destinationString.append(primaryString.codePointAt(i));
                
            }
        }
        
        return destinationString.toString().toLowerCase();
        
    }
    
    private static String sort(String string) {
        
        char[] charArray = string.toCharArray();
        Arrays.sort(charArray);
        return new String(charArray);
        
    }
    
    private static boolean areAnagram(String clear, String scrambled) {
        
        clear = sort(clear);
        scrambled = sort(scrambled);
        
        return clear.equals(scrambled);
    }
    
    private static void outputResult(boolean result) {
        
        if (result) {
            System.out.println("ARE anagrams!");
        } else {
            System.out.println("NOT anagrams!");
        }
        
    }
}
